package com.linln.modules.store.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.constant.NoticeTypeConst;
import com.linln.common.constant.ShenheStatusConst;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.domain.Notice;
import com.linln.modules.member.service.MembernoticeService;
import com.linln.modules.member.service.NoticeService;
import com.linln.modules.store.domain.Storeinfo;
import com.linln.modules.store.repository.StoreinfoRepository;
import com.linln.modules.store.service.StoreinfoService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class StoreinfoServiceImpl implements StoreinfoService {

    @Autowired
    private StoreinfoRepository baseinfoRepository;
    @Autowired
    private MembernoticeService membernoticeService;


    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public Storeinfo getById(Long id) {
        return baseinfoRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<Storeinfo> getPageList(Example<Storeinfo> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return baseinfoRepository.findAll(example, page);
    }

    @Override
    public  Page<Storeinfo> getPageList(Example<Storeinfo> example,Integer pageNumber,Integer pageSize){
    		Sort sort = new Sort(Sort.Direction.DESC, "id");
    	
    	 if(pageNumber==null) {
    		 pageNumber=1;
         }
    	PageRequest page = PageSort.pageRequest(pageNumber,pageSize,sort);      
    	return baseinfoRepository.findAll(example, page);
    
    }
    
    @Override
    public  Page<Storeinfo> getPageList(Example<Storeinfo> example,Sort sort,Integer pageNumber,Integer pageSize){
    	if(sort==null) {
    	 sort = new Sort(Sort.Direction.DESC, "id");
    	}
    	 if(pageNumber==null) {
    		 pageNumber=1;
         }
    	PageRequest page = PageSort.pageRequest(pageNumber,pageSize,sort);      
    	return baseinfoRepository.findAll(example, page);
    
    }

    /**
     * 根据id查询所有信息
* @param ids
* @return
*/
    public List<Storeinfo> getList(List<Long> ids){
    	return baseinfoRepository.findAllById(ids);
    }
    
    /**
     * 保存数据
     * @param baseinfo 实体对象
     */
    @Override
    public Storeinfo save(Storeinfo baseinfo) {
        return baseinfoRepository.save(baseinfo);
    }
    
    @Override
    public Storeinfo saveAndSendNotice(Storeinfo baseinfo) {

    	Storeinfo baseinfonew=baseinfoRepository.save(baseinfo);
	/*if(baseinfonew!=null && baseinfonew.getId()>0) {
		//发送消息
		membernoticeService.updateIsread(2,baseinfonew.getId());
 		//ok发送消息
		Notice notice=new Notice();
		notice.setArea(baseinfo.getArea());
		notice.setContent("一条待审核信息");
        notice.setCreateDate(new Date());
        notice.setMember(baseinfo.getMember());
        notice.setType(NoticeTypeConst.MESSAGE);
        notice.setTypeId(baseinfonew.getId());
        notice.setIsread(0); 
		noticeService.save(notice);
 	}*/
	return baseinfonew;

    }
    /**
     * 编辑数据
     * @param baseinfo 实体对象
     */
   public  Storeinfo update(Storeinfo baseinfo){
        return baseinfoRepository.saveAndFlush(baseinfo);
   }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return baseinfoRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
    
    /**
     * 审核状态
     */
    @Override
    @Transactional
    public Boolean updateStoreStatus(Integer storeStatus, Long  id) {
    	
    	Storeinfo baseinfo=baseinfoRepository.getOne(id);
    	if( baseinfoRepository.updateStoreStatus(storeStatus, id) > 0) {
    			//ok发送消息
    	    		Membernotice membernotice=new Membernotice();
    	    		membernotice.setArea(baseinfo.getArea());
    	    		membernotice.setTypeid(id);
    	    		if(storeStatus==ShenheStatusConst.OK) {
    	    			membernotice.setContent(baseinfo.getStore_name());
    	        		
    	    		}else if(storeStatus==ShenheStatusConst.REFUSE) {
    	    			membernotice.setContent(baseinfo.getStore_name());
                  	}
    	    		membernotice.setIsread(0); 
    	    		membernotice.setCreateDate(new Date());
    	    		membernotice.setMember(baseinfo.getMember());
    	    		membernotice.setNoticestatus(storeStatus);
    	    		membernotice.setType(NoticeTypeConst.STORE);
    	    		membernoticeService.save(membernotice);
    	    		return true;
    }else {
    	     return false;
    		}
    }
    
    /**
     * 信息的阅读数增加1
     */
    @Override
  public   Boolean updateIsReadAddById(Long id) {
    	return baseinfoRepository.updateIsReadAddById(id)>0;
    }
    
    /**
     * 信息的分享数加1
     */
    @Override
  public   Boolean updateIsShareAddById(Long id) {
    	return baseinfoRepository.updateIsShareAddById(id)>0;
    }
    @Override
    public Boolean updateReplyAddById(Long id) {
    	return baseinfoRepository.updateReplyAddById(id)>0;

    }

    /**
     * 收藏数加1
     * @param id
     * @return
     */
    public  Boolean updateIsCollectAddById(Long id) {
    	return baseinfoRepository.updateIsCollectAddById(id)>0;
    }

	/**
	 * 收藏数减1
	 * @param id
	 * @return
	 */
    public Boolean updateIsCollectSubById(Long id) {
    	return baseinfoRepository.updateIsCollectSubById(id)>0;
    }
    
    /**
     * 赞数加1
     * @param id
     * @return
     */
    public Boolean updateIsZanAddById(Long id) {
    	return baseinfoRepository.updateIsZanAddById(id)>0;
    }

	/**
	 * 赞数减1
	 * @param id
	 * @return
	 */
    public Boolean updateIsZanSubById(Long id) {
    	return baseinfoRepository.updateIsZanSubById(id)>0;
    }
    
    public Integer getCountByArea(String area) {
    	return baseinfoRepository.getCountByArea(area);
    }
    
    public      Integer getCountByAreaAndToday(String area) {
        return baseinfoRepository.getCountByAreaAndToday( area);

    }

   public long  getAllCount(Example<Storeinfo> example) {
	   return baseinfoRepository.count(example);
   }

    
    public List<Storeinfo> queryListByArea(String area){
		return baseinfoRepository.queryListByArea(area);
	}
    
    public  Storeinfo findByOrderId(String orderId) {
		return baseinfoRepository.findByOrderId(orderId);

    }
    
    
    public Integer updatePayStatus( String orderId) {
		return baseinfoRepository.updatePayStatus(orderId);

    }
    public Integer updateRefundStatus( String orderId) {
  		return baseinfoRepository.updateRefundStatus(orderId);

      }
    
    public Integer updateStoreStar(Integer storeStar,Integer readnum, Long id) {
  		return baseinfoRepository.updateStoreStar(storeStar,readnum,id);

    }


    public List<Storeinfo> queryListByMemberId(Long memberid){
  		return baseinfoRepository.queryListByMemberId(memberid);
    }
    /**
     * 代理商查看未过期的信息
     * @param area
     * @return
     */
    public List<Storeinfo> queryListByAreaNew(String area){
  		return baseinfoRepository.queryListByAreaNew(area);

    }


	

}